package eu.tanov.epf.pv.types.project.ui.pages;

import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableColumn;

import eu.tanov.epf.pv.types.project.ui.ProjectActivator;

public class MethodAndPracticePage extends WizardPage {
	private static final Image CHECKED = ProjectActivator.imageDescriptorFromPlugin(ProjectActivator.PLUGIN_ID, "icons/checked.gif").createImage();
	private static final Image UNCHECKED = ProjectActivator.imageDescriptorFromPlugin(ProjectActivator.PLUGIN_ID, "icons/unchecked.gif")
			.createImage();
	private int selectedMethodIndex = 0;
	private TableViewer viewer;
	protected ProjectIterationWizardData data;

	protected MethodAndPracticePage(String pageName, ProjectIterationWizardData data) {
		super(pageName);
		this.data = data;
		setTitle(pageName);
		setDescription("Select the project methods and practices");
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		setControl(container);
		createCombo(container);
		createPracticesTable(container);
	}

	private void createPracticesTable(Composite container) {
		viewer = new TableViewer(container, SWT.FULL_SELECTION);
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;

		viewer.getTable().setLayoutData(gridData);
		viewer.getTable().setHeaderVisible(true);
		TableViewerColumn checkboxColumn = createTableViewer(viewer, "!", 50);
		checkboxColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				return null;
			}

			@Override
			public Image getImage(Object element) {
				if (((SelectableMethodPractice) element).isSelected()) {
					return CHECKED;
				} else {
					return UNCHECKED;
				}
			}
		});
		checkboxColumn.setEditingSupport(new CheckBoxEditingSupport(viewer));
		TableViewerColumn contentColumn = createTableViewer(viewer, "Practice", 200);
		contentColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				SelectableMethodPractice practice = (SelectableMethodPractice) element;
				return practice.getName();
			}
		});
		viewer.setContentProvider(new ArrayContentProvider());
		viewer.setInput(getInput());
	}

	public IWizardPage getNextPage() {
		IWizardPage next = super.getNextPage();

		if (next instanceof FeaturedPracticesFromIterationsPage) {
			((FeaturedPracticesFromIterationsPage) next).updatePage();
		}

		return next;
	}

	private Object getInput() {
		String currentMethod = data.getProjectMethods()[selectedMethodIndex];
		List<SelectableMethodPractice> list = data.getMethodPractices(currentMethod);
		return list.toArray();
	}

	protected TableViewerColumn createTableViewer(TableViewer viewer, String text, int width) {
		TableViewerColumn columnViewer = new TableViewerColumn(viewer, SWT.NONE);
		final TableColumn column = columnViewer.getColumn();
		column.setText(text);
		column.setWidth(width);
		column.setResizable(true);
		column.setMoveable(true);
		return columnViewer;
	}

	protected void createCombo(final Composite container) {
		final Label methodLabel = new Label(container, SWT.NONE);
		methodLabel.setText("Choose project method");
		final Combo methodCombo = new Combo(container, SWT.NONE | SWT.READ_ONLY);
		methodCombo.setItems(data.getProjectMethods());
		methodCombo.select(0);
		methodCombo.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				Combo combo = (Combo) e.getSource();
				selectedMethodIndex = combo.getSelectionIndex();
				viewer.setInput(getInput());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
	}

}
